package client.comm.baoding.ui

import android.content.Intent
import client.comm.commlib.toast.toast
import client.comm.baoding.R
import client.comm.baoding.databinding.ActivityCreaterechargeBinding
import client.comm.baoding.ui.vm.CreateRechargeViewModel
import client.comm.baoding.BaseMiddleActivity
import client.comm.baoding.dialog.PayDetailDialog
import com.blankj.utilcode.util.LogUtils

class CreateRechargeActivity :
    BaseMiddleActivity<CreateRechargeViewModel, ActivityCreaterechargeBinding>() {
    override fun getLayout(): Int {
        return R.layout.activity_createrecharge
    }

    override fun initViewModel(): CreateRechargeViewModel = createViewModel()

    override fun initView() {
        binding.event = this
        binding.vm = viewModel
    }

    override fun initData() {
//        viewModel.callUserInfo()
    }

    override fun observerUI() {
        viewModel.okCall.observe(this, {
            it?.let {
                if (it.isSuccess) {
                    if (viewModel.txType.value == 1) {
                        startActivity(Intent(this, RechargeActivity::class.java))

                    } else if (viewModel.txType.value == 3) {
                        //新充值银行卡 方式 w_type =3
                        it.data?.let {
                            startActivity(Intent(this, Bank3CodeActivity::class.java).apply {
                                putExtra("bank_bind", it)
                                putExtra("status", "2")
                            })
                        }

                    } else if (viewModel.txType.value == 4) {
//                        LogUtils.i(it.data)
                        val paymentUrl1 = it.data!!.pay_url
                        paymentUrl1?.let {
                            val intent = Intent(this, WebPayActivity::class.java)
                                .apply {
                                    putExtra("adInfo", it)
                                    putExtra("title", "支付")
                                }
                            startActivity(intent)

                        }
                    }
                    finish()

                }
            }
        })
    }

    fun ok() {
        if (viewModel.num.value == null || viewModel.txType.value!! < 0) {
            toast("所有不得为空")
            return
        }
        if (viewModel.txType.value == 3) {
            PayDetailDialog(this, null).apply {
                vm = viewModel
                payMoney.value = "${viewModel.num.value}"

                onSelectBank = { money: String, bankNo: String, linkId: String ->
                    LogUtils.i("${money + " - " + bankNo + " - " + linkId}")
                    viewModel.ok3(linkId)
                }

            }.show()

        } else { //w_type=4 通道2
            viewModel.ok()
        }

    }

    fun goRecord() {
        startActivity(Intent(this, RechargeOrderActivity::class.java)) //RechargeRecordActivity
    }
}